package com.example.dllo.keepdemo.welcome;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.base.BaseActivity;
import com.example.dllo.keepdemo.base.BaseApplication;
import com.example.dllo.keepdemo.main.MainActivity;

import java.io.IOException;
import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;

/**
 * Created by dllo on 16/1/25.
 */
public class WelcomeActivity extends BaseActivity implements SurfaceHolder.Callback, MediaPlayer.OnCompletionListener, View.OnClickListener {
    private SurfaceHolder surfaceHolder;
    private SurfaceView surfaceView;
    private MediaPlayer mediaPlayer;
    private TextView tvGo;
    private ImageView imgQQ,imgQZone,imgSina;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShareSDK.initSDK(this);
        setContentView(R.layout.activity_welcome);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceview_activity_welcome);
        tvGo = (TextView) findViewById(R.id.tv_activity_welcome_go);
        imgQQ = (ImageView) findViewById(R.id.img_activity_welcome_qqlogin);
        imgQZone = (ImageView) findViewById(R.id.img_activity_welcome_qqzonelogin);
        imgSina = (ImageView) findViewById(R.id.img_activity_welcome_sinalogin);

        imgQQ.setOnClickListener(this);
        imgQZone.setOnClickListener(this);
        imgSina.setOnClickListener(this);
        tvGo.setOnClickListener(this);

        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // 当SurfaceView中的Surface被创建的时候被调用

        //然后指定需要播放文件的路径，初始化MediaPlayer
        //在指定了MediaPlayer播放的容器后，我们就可以使用prepare或者prepareAsync来准备播放了

        //本地路径
        try {
            mediaPlayer.setDataSource(WelcomeActivity.this,Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.intro));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //在这里我们指定MediaPlayer在当前的Surface中进行播放
        mediaPlayer.setDisplay(surfaceHolder);

        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }

        mediaPlayer.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // 当Surface尺寸等参数改变时触发
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Activity销毁时停止播放，释放资源。不做这个操作，即使退出还是能听到视频播放的声音
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        mediaPlayer.release();
    }

    //播放完执行
    @Override
    public void onCompletion(MediaPlayer mp) {
        // 当MediaPlayer播放完成后触发
        mediaPlayer.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_activity_welcome_qqlogin :

                PlatformActionListener listener = new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        Log.d("welcome", "12312312312312312312312312312312312312");
                        Log.d("TrainFragment", platform.getDb().getUserId());
                        Log.d("TrainFragment", platform.getDb().getUserName());
                        Log.d("TrainFragment", platform.getDb().getUserIcon());
                        Log.d("TrainFragment", "12312312312312312312312312312312312312");
                        Log.d("TrainFragment", "hashMap:" + hashMap);

                        Intent intent1 = new Intent(WelcomeActivity.this,MainActivity.class);
                        intent1.putExtra("username",platform.getDb().getUserName());
                        intent1.putExtra("usericon",platform.getDb().getUserIcon());
                        startActivity(intent1);

                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        Toast.makeText(WelcomeActivity.this, "授权失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        Toast.makeText(WelcomeActivity.this, "取消授权", Toast.LENGTH_SHORT).show();
                    }
                };

                Platform platform = ShareSDK.getPlatform(WelcomeActivity.this, QQ.NAME);
                //判断是否已经授权过，如果授权，删除授权信息重新授权
                if (platform.isValid()) {
                    platform.removeAccount();
                }
                platform.setPlatformActionListener(listener);
                platform.showUser(null);
                platform.authorize();


                break;
            case R.id.img_activity_welcome_qqzonelogin :
                PlatformActionListener listener1 = new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        Log.d("welcome", "12312312312312312312312312312312312312");
                        Log.d("TrainFragment", platform.getDb().getUserId());
                        Log.d("TrainFragment", platform.getDb().getUserName());
                        Log.d("TrainFragment", platform.getDb().getUserIcon());
                        Log.d("TrainFragment", "12312312312312312312312312312312312312");
                        Log.d("TrainFragment", "hashMap:" + hashMap);

                        Intent intent1 = new Intent(WelcomeActivity.this,MainActivity.class);
                        intent1.putExtra("username",platform.getDb().getUserName());
                        intent1.putExtra("usericon",platform.getDb().getUserIcon());
                        startActivity(intent1);
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        Toast.makeText(WelcomeActivity.this, "授权失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        Toast.makeText(WelcomeActivity.this, "取消授权", Toast.LENGTH_SHORT).show();
                    }
                };

                Platform platform1 = ShareSDK.getPlatform(WelcomeActivity.this, QZone.NAME);
                //判断是否已经授权过，如果授权，删除授权信息重新授权
                if (platform1.isValid()) {
                    platform1.removeAccount();
                }
                platform1.setPlatformActionListener(listener1);
                platform1.showUser(null);
                platform1.authorize();
                break;
            case R.id.img_activity_welcome_sinalogin :
                PlatformActionListener listener2 = new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        Log.d("welcome", "12312312312312312312312312312312312312");
                        Log.d("TrainFragment", platform.getDb().getUserId());
                        Log.d("TrainFragment", platform.getDb().getUserName());
                        Log.d("TrainFragment", platform.getDb().getUserIcon());
                        Log.d("TrainFragment", "12312312312312312312312312312312312312");
                        Log.d("TrainFragment", "hashMap:" + hashMap);

                        Intent intent1 = new Intent(WelcomeActivity.this,MainActivity.class);
                        intent1.putExtra("username", platform.getDb().getUserName());
                        intent1.putExtra("usericon", platform.getDb().getUserIcon());
                        startActivity(intent1);
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        Toast.makeText(WelcomeActivity.this, "授权失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        Toast.makeText(WelcomeActivity.this, "取消授权", Toast.LENGTH_SHORT).show();
                    }
                };

                Platform platform2 = ShareSDK.getPlatform(WelcomeActivity.this, QQ.NAME);
                //判断是否已经授权过，如果授权，删除授权信息重新授权
                if (platform2.isValid()) {
                    platform2.removeAccount();
                }
                platform2.setPlatformActionListener(listener2);
                platform2.showUser(null);
                platform2.authorize();
                break;
            case R.id.tv_activity_welcome_go :
                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                mediaPlayer.stop();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }
}
